/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare.plugins');
(function() {
    var EVENTS = net.autodata.EventBus.EVENTS;

    var Plugin = Ext.extend(Ext.util.Observable, {

        panel: undefined,

        init: function(panel) {
            this.panel = panel;

            var eb = net.autodata.EventBus.getInstance();
            eb.on(EVENTS.READY, this.onCompetitorChanged, this);
            eb.on(EVENTS.SESSION_COMPETITOR_CHANGED, this.onCompetitorChanged, this);
            eb.on(EVENTS.SESSION_COMPETITOR_REMOVED, this.onCompetitorChanged, this);
        },

        onCompetitorChanged: function() {
            var session = net.autodata.Session.getInstance();
            var vehicles = session.vehicles;

            if (vehicles && vehicles.hasCompetitors()) {
                this.panel.show();
                this.panel.setVehicles(vehicles.getBaseVehicle(), vehicles.getCompetitors().getRange());
                this.panel.loadToolbar();
                return;
            }

            this.panel.hide();
        },

        onDestroy: function() {
            var eb = net.autodata.EventBus.getInstance();
            eb.un(EVENTS.READY, this.onCompetitorChanged, this);
            eb.un(EVENTS.SESSION_COMPETITOR_CHANGED, this.onCompetitorChanged, this);
            eb.un(EVENTS.SESSION_COMPETITOR_REMOVED, this.onCompetitorChanged, this);
        }
    });

    net.autodata.compare.plugins.AdvantagesPanelEventBusConnector = Plugin;
})();